package org.hades.corelib.base.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.appcompat.widget.Toolbar
import org.hades.corelib.R

class TitleBar : Toolbar {
    protected var centerText: String? = ""
    @ColorInt
    protected var textColor = 0
    protected var textSize = 15

    protected lateinit var tvCenter: TextView

    constructor(context: Context) : super(context) {
        init(context, null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context, attrs)
    }

    protected fun init(context: Context, attrs: AttributeSet?) {
        LayoutInflater.from(context).inflate(R.layout.base_title_bar, this)
        tvCenter = findViewById(R.id.tv_center)
        val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.TitleBar, 0, 0)
        try {
            centerText = typedArray.getString(R.styleable.TitleBar_title_text)
            textColor = typedArray.getColor(R.styleable.TitleBar_textColor, textColor)
            textSize = typedArray.getDimensionPixelOffset(R.styleable.TitleBar_textSize, textSize)
        } finally {
            typedArray.recycle()
        }
        setText(centerText);
        setCenterTextColor(textColor);
        tvCenter.paint.textSize = textSize.toFloat();

    }


    fun setText(text: String?) {
        centerText = text
        text.orEmpty()
        tvCenter.text = centerText?:""
    }

    fun setCenterTextColor(@ColorInt color: Int) {
        textColor = color
        tvCenter.setTextColor(color)
    }

    fun setCenterTextSize(size:Int){
        textSize = size
        tvCenter.textSize = textSize.toFloat()
    }
}